import { useEffect, useState } from "react";

export function useRequest<T = any>(request: Function) {
	const [loading, setLoading] = useState(true)
	const [error, setError] = useState(null)
	const [data, setData] = useState<{ code: number, data: T, message: string }>();

	useEffect(() => {
		request()
			.then((resp: any) => {
				setData(resp.data)
			})
			.catch((error: any) => {
				setError(error)
			})
			.finally(() => {
				setLoading(false)
			})
	}, [])

	return {
		loading, error, data
	}
}